package com.nefu.editor.core.mapper.provider;

import com.nefu.editor.core.model.User;
import com.nefu.editor.core.model.Vo.UserVo;
import org.apache.ibatis.jdbc.SQL;

/**
 * @author chenchenT
 * @date : 18-11-12
 * @since : java 8
 */
public class UserProvider {

    public String selectUserByCondition(User user) {
        return new SQL() {
            {
                SELECT("user_systemId as systemId, user_idNumber as idNumber, user_password as password, user_sex as sex, "
                        + " user_email as email, user_type as type, writer_style as writerStyle, writer_unitPrice as writerUnitPrice");
                FROM("t_user");
                if (null != user.getIdNumber()) {
                    WHERE("user_idNumber=#{idNumber}");
                }
                if (null != user.getPassword()) {
                    WHERE("user_password=#{password}");
                }
                if (null != user.getSex()) {
                    WHERE("user_sex=#{sex}");
                }
                if (null != user.getType()) {
                    WHERE("user_type=#{type}");
                }
                if (null != user.getToken()) {
                    WHERE("user_token=#{token}");
                }
                if (null != user.getWriterStyle()) {
                    WHERE("writer_style=#{writerStyle}");
                }
                if (null != user.getWriterUnitPrice()) {
                    WHERE("writer_unitPrice=#{writerUnitPrice}");
                }

            }
        }.toString();
    }

    public String selectByUserSystemId(UserVo userVo) {
        return new SQL() {
            {
                SELECT("user_systemId As systemId, user_idNumber as idNumber, user_password as password, user_sex as sex, "
                        + " user_email as email, user_token as token, user_type as type");
                FROM("t_user");
                WHERE("user_systemId=#{systemId}");
            }
        }.toString();
    }

    public String insertNewUser(User user) {
        return new SQL() {
            {
                INSERT_INTO("t_user");
                if (null != user.getIdNumber() && null != user.getPassword()) {
                    VALUES("user_idNumber",user.getIdNumber());
                    VALUES("user_password", user.getPassword());
                    if (null != user.getSex()) {
                        VALUES("user_sex", user.getSex());
                    }
                    if (null != user.getEmail()) {
                        VALUES("user_email", user.getEmail());
                    }
                    if (null != user.getType()) {
                        VALUES("user_type", user.getType());
                    }
                }
            }
        }.toString();
    }

}
